<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="userId"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        highlight-current-row
        stripe
        border
      >
        <template #realname="{ row }">
          <span class="pointer blue" @click="showStudentDetail(row)">
            {{ row.student_name }}
          </span>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <edit v-model="showEdit" :data="current" @done="reload" />
    <!-- 学生详情 -->
    <detail v-model="showDetail" :student-id="currentId" @done="reload" />
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import Search from './components/search.vue';
  import Edit from './components/edit.vue';
  import Detail from '@/views/student/components/detail.vue';
  import { pageStudentVisit } from '@/api/student';

  /** 表格实例 */
  const tableRef = ref(null);

  /** 表格列配置 */
  const columns = ref([
    {
      prop: 'cate',
      label: '分类',
      width: 130
    },
    {
      prop: 'student_name',
      label: '姓名',
      width: 100,
      slot: 'realname'
    },
    {
      prop: 'visit_user',
      label: '回访人',
      width: 100
    },
    {
      prop: 'content',
      label: '回访内容'
    },
    {
      prop: 'visit_date',
      label: '回访时间',
      width: 140
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 当前编辑数据 */
  const currentId = ref(null);

  /** 是否显示编辑弹窗 */
  const showEdit = ref(false);

  /** 是否显示编辑弹窗 */
  const showDetail = ref(false);

  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageStudentVisit({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 打开编辑弹窗 */
  const openEdit = (row) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  /** 打开编辑弹窗 */
  const showStudentDetail = (row) => {
    currentId.value = row.id ?? 0;
    showDetail.value = true;
  };
</script>

<script>
  export default {
    name: 'SystemUser'
  };
</script>
